package org.example.demo.common.convert;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

public class RabbitMessageConverter implements MessageConverter {
    private final GenericMessageConverter messageConverter;

    public RabbitMessageConverter(GenericMessageConverter messageConverter) {
        this.messageConverter = messageConverter;
    }

    @Override
    public org.example.demo.domain.Message fromMessage(Message message) throws MessageConversionException {
        return (org.example.demo.domain.Message) this.messageConverter.fromMessage(message);
    }

    @Override
    public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
        org.example.demo.domain.Message msg = (org.example.demo.domain.Message) object;
        messageProperties.setDelay(msg.getDelayMills());
        return this.messageConverter.toMessage(msg, messageProperties);
    }
}
